<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace switcher\renderers;

/**
 * The <b>JsonRenderer</b> class encodes the context (the Model part of the MVC pattern)
 * into a JSON string. It is useful for integration with AJAX. It might also be used to
 * expose public API.
 * 
 * <p>XML declaration :
 * <code>
 * <renderer name="myJson" type="switcher\renderers\JsonRenderer"/>
 * </code>
 * 
 * It is possible to change the content type :
 * <code>
 * <renderer name="myJson" type="switcher\renderers\JsonRenderer">
 *     <contentType>application/json</contentType>
 * </renderer>
 * </code>
 * </p>
 * 
 * @author Dri <switcher.framework@gmail.com>
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 * @package switcher\renderers
 * @since 1.0.0
 * @see Renderer
 */
final class JsonRenderer extends \switcher\renderers\Renderer
{
	/**
	 * The default content type
	 * @var string
	 */
	const DEFAULT_CONTENT_TYPE = 'text/json';
	
	/**
	 * No-arg constructor.
	 */
	public function __construct()
	{
		$this->contentType = self::DEFAULT_CONTENT_TYPE;
	}
	
	/**
	 * Encodes the context into a JSON string.
	 * 
	 * @return string A JSON string
	 */
	public function doRendering()
	{
		return json_encode($this->context);
	}
}

?>